package cn.bdqn.service.Imp;

import cn.bdqn.dao.Imp.UserdaoImp;
import cn.bdqn.dao.Usersdao;
import cn.bdqn.entity.Users;
import cn.bdqn.service.Usersservice;
import cn.bdqn.utils.Baseutils;
import cn.bdqn.utils.PageBean;

import java.util.List;

/**
 * @author 梁非凡
 * @date 2018/8/13 16:58
 */
public class UsersserviceImp implements Usersservice {
    Usersdao usersdao=new UserdaoImp();
    @Override
    public Users findbyidandname(String name, String password) {
        try {
            Users users = usersdao.findbyidandname(name,password);
            return users;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public boolean amend(String pwd, Integer id) {
        try {
            Baseutils.begin();
            Integer count = usersdao.amend(pwd, id);
            return count==1?true:false;
        } catch (Exception e) {
            try {
                Baseutils.rollback();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
            return false;
        }finally {
            try {
                Baseutils.comm();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public boolean logout(Users users) {
        try {
            Baseutils.begin();
            Integer i = usersdao.logout(users);
            return i==1?true:false;
        } catch (Exception e) {
            try {
                Baseutils.rollback();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
            return false;
        }finally {
            try {
                Baseutils.comm();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public PageBean<Users> findpageByname(String name, Integer startIndex, Integer pageSize) {
        PageBean<Users> pageBean= null;
        try {
            Integer totalCount= usersdao.findAll(name);
            pageBean = new PageBean(startIndex,pageSize,totalCount);
            List<Users> list= usersdao.findpageByname(name,pageBean.getStartIndex(),pageBean.getPageSize());
            pageBean.setList(list);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        return pageBean;
    }
}
